package
{
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.sampler.NewObjectSample;
	
	import org.flixel.*;

	public class FriendRoomState extends FlxState
	{
		private static const TOP_BAR_WIDTH:int = 700;
		private static const TOP_BAR_HEIGHT:int = 80;
		private static const TOP_BAR_X:int = 0;
		private static const TOP_BAR_Y:int = 0;
		
		private static const TITLE_X:int = 140;
		private static const IMAGE_X:Number = 32;
		private static const IMAGE_Y:Number = 17.75;
		
		private static const ACHIEV_BAR_WIDTH:int = 120;
		private static const ACHIEV_BAR_HEIGHT:int = 400;
		private static const ACHIEV_BAR_X:int = 0;
		private static const ACHIEV_BAR_Y:int = 100;
		
		private var friendID:String;
		private var friendName:String;
		
		private var puzzleLoader:MiniPuzzleLoader;
		protected var topBar:FlxSprite;
		protected var title:FlxText;
		protected var achievBar:FlxSprite;
		
		protected var achievementComponent:PlayerAchievementComponent;
		protected var achievementPopup:FlxGroup;
		
		protected var friendStats:AchievementSystem;
		
		protected var loading:Boolean;
		protected var achievInitialized:Boolean;
		
		private var exitButton:FlxSprite;
		private var musicIcon:Music;
		
		
		public function FriendRoomState(id:String, name:String)
		{
			friendID = id;
			friendName = name;
			
			add(new FlxSprite(0,0, Res.bg));
			createTitleBar();
			
			this.add(new FlxSprite(50, topBar.height + 50, Res.ChallengeBar));
			
			Client.registerUser(id, updateAchievements);
			achievementPopup = new FlxGroup();
			
			createPuzzleLoader();
			
			exitButton = new FlxSprite(0, 0, Res.ExitButton);
			exitButton.x = FlxG.width - exitButton.width - 5;
			exitButton.y = 5;
			
			//add(Elemental.friendBar);
			musicIcon = new Music(676,50);
			Elemental.Log.logEnterFriendRoom(name,id);
		}
		
		private function updateAchievements(e:Event):void {
			trace("friend stats complete");
			//for achievements
			var stats:String = e.target.data;
			//trace(Elemental.fbInfo.userID + " " + Elemental.achievements.getStoryLevelReached() + ":" + Elemental.achievements.getMonstersKilled() + ":" + Elemental.achievements.getChickensThrown() + ":" + Elemental.achievements.getChallengesBeaten() + ":" + Elemental.achievements.getChallengesIssued() + ":");
			//trace(friendID + " " + stats);
			if (stats.charAt(0) != "E") {
				friendStats = new AchievementSystem(true);
				var statArray:Array = stats.split(":");
				if (statArray[10] > 3) {
					friendStats.initializeWithStats(
						statArray[0], //level
						statArray[1], //monsters
						statArray[2], //chickens
						statArray[3], //challenges beaten
						statArray[4], //challenges issued
						statArray[5], //pacifist
						statArray[6], //maps created
						statArray[7], //maps beated
						statArray[8], //friends beaten
						statArray[9]); //invites
				}
				createAchievementsBar();
				achievInitialized = true;
			}
		}
		
		public function createPuzzleLoader():void {
			puzzleLoader = new MiniPuzzleLoader(FlxG.width - MiniPuzzleLoader.CONTAINER_WIDTH - 40, topBar.y + topBar.height + 50, friendName,friendID);
			add(puzzleLoader);
		}
		
		public function imgLoaded(e:Event):void {
			var img:Bitmap = new Bitmap(e.target.content.bitmapData);
			var friendImage:FlxSprite = new FlxSprite(200,10);
			friendImage.pixels = img.bitmapData;
			friendImage.x = IMAGE_X;
			friendImage.y = IMAGE_Y;
			add(friendImage);
			this.remove(topBar,true);
			this.remove(title, true);
			add(topBar);
			add(title);
			add(exitButton);
			loading = false;
			add(musicIcon);
		}
		
		protected function createTitleBar():void {
			topBar = new FlxSprite(0, 0, Res.Titlebar);
			
			loading = true;
			Elemental.fbInfo.addPicture(friendID, imgLoaded);
			
			title = new FlxText(TITLE_X, 0, TOP_BAR_WIDTH - 100, friendName + "'s Room", false);
			title.setFormat("Georgia", 40, 0x000000, "left", 0, true);
			title.y = (topBar.height - title.height) / 2 - 5;
			this.add(topBar);
			this.add(title);
		}
		
		protected function createAchievementsBar():void {
			achievementComponent = new PlayerAchievementComponent(50, topBar.height + 50, true, friendStats);
			this.add(achievementComponent);
		}
		
		private function updateAchievementOverlap():void {
			if (achievInitialized && achievementComponent.hasPopup()) {
				this.remove(achievementPopup, true);
				achievementPopup = achievementComponent.getNextPopup();
				this.add(achievementPopup);
			}
			if (achievInitialized && !achievementComponent.isStillOverlapping()) {
				this.remove(achievementPopup, true);
			}
		}
		
		public override function update():void {
			super.update();
			musicIcon.updateMusic();
			updateAchievementOverlap();
			
			if (FlxG.mouse.justPressed() && exitButton.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY) && !loading) {
				FlxG.switchState(new PlayerRoomState());
			}
		}
		
		/**
		 * Especially remove the friendbar because FlxG.switchState will destroy everything
		 * that's currently added to the screen.
		 */
		public override function destroy():void {
			remove(Elemental.friendBar, true);
			super.destroy();
		}
	}
}